package com.hnxxxy.workshop.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnxxxy.workshop.mapper.FactoryMapper;
import com.hnxxxy.workshop.model.FactoryDO;
import com.hnxxxy.workshop.request.FactoryPageRequest;
import com.hnxxxy.workshop.request.FactoryRequest;
import com.hnxxxy.workshop.service.FactoryService;
import com.hnxxxy.workshop.vo.FactoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 工厂 服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-05-09
 */
@Service
@Slf4j
public class FactoryServiceImpl implements FactoryService {


    @Resource
    private FactoryMapper factoryMapper;

    /**
     * 新建工厂
     * @param factoryRequest
     */
    @Override
    public void add(FactoryRequest factoryRequest) {

        FactoryDO factoryDO = new FactoryDO();
        BeanUtils.copyProperties(factoryRequest, factoryDO);

        int rows = factoryMapper.insert(factoryDO);
        log.info("新增消息:rows={},data={}",rows,factoryDO);
    }

    /**
     * 根据id删除工厂
     * @param id
     */
    @Override
    public void deleteById(int id) {
        int rows = factoryMapper.deleteById(id);

        log.info("删除消息条数 :rows={}",rows);
    }

    /**
     * 修改工厂信息
     * @param factoryRequest
     */
    @Override
    public void update(FactoryRequest factoryRequest) {
        FactoryDO factoryDO = new FactoryDO();
        BeanUtils.copyProperties(factoryRequest, factoryDO);
        int rows = factoryMapper.updateById(factoryDO);
        log.info("修改消息:rows={}",rows);
    }

    /**
     * 查询全部工厂
     * @return
     */
    @Override
    public List<FactoryVO> findAllFactory() {
        List<FactoryDO> factoryDOList = factoryMapper.selectList(null);

        List<FactoryVO> factoryVOList = factoryDOList.stream().map(obj -> {
            FactoryVO factoryVO = new FactoryVO();
            BeanUtils.copyProperties(obj, factoryVO);
            return factoryVO;
        }).collect(Collectors.toList());

        log.info("全部工厂:factoryVOList={}",factoryVOList);
        return factoryVOList;
    }

    /**
     * 根据id查询工厂
     * @param id
     * @return
     */
    @Override
    public FactoryVO findByIdFactory(int id) {
        FactoryVO factoryVO = new FactoryVO();
        FactoryDO factoryDO = factoryMapper.selectById(id);

        BeanUtils.copyProperties(factoryDO, factoryVO);
        log.info("根据id查询信息:id={}  factoryVO={}",id,factoryVO);
        return factoryVO;
    }

    @Override
    public Map<String, Object> page(FactoryPageRequest factoryPageRequest) {
        int page = factoryPageRequest.getPage();
        int size = factoryPageRequest.getSize();
        String name = factoryPageRequest.getName();
        String manager = factoryPageRequest.getManager();

        QueryWrapper<FactoryDO> queryWrapper = new QueryWrapper<FactoryDO>();

        if (!StrUtil.hasEmpty(name)){
            queryWrapper.like("name", name);
        }
        if (!StrUtil.hasEmpty(manager)){
            queryWrapper.like("manager", manager);
        }

        Page<FactoryDO> pageInfo = new Page<>(page, size);

        IPage<FactoryDO> factoryDOIPage = factoryMapper.selectPage(pageInfo,queryWrapper);

        Map<String,Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", factoryDOIPage.getTotal());
        pageMap.put("total_page", factoryDOIPage.getPages());
        pageMap.put("current_data",factoryDOIPage.getRecords()
                .stream().map(obj->beanProocess(obj)).collect(Collectors.toList()));

        log.info("分页消息:pageMap={}",pageMap);
        return pageMap;
    }

    private FactoryVO beanProocess(FactoryDO obj) {
        FactoryVO factoryVO = new FactoryVO();
        BeanUtils.copyProperties(obj, factoryVO);
        return factoryVO;
    }

}
